//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

import ants.p2p.utils.indexer.*;
import ants.p2p.*;
import ants.p2p.filesharing.*;

import javax.swing.*;
import java.awt.event.*;

import org.apache.log4j.*;

import java.awt.Toolkit;
import java.awt.datatransfer.*;

import java.io.*;

public class SharePopupMenu extends JPopupMenu {
  ShareAntPanel callerShare;
  FileInfos fileInfos;

  static Logger _logger = Logger.getLogger(SharePopupMenu.class.getName());
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenuItem jMenuItem2 = new JMenuItem();
  JMenuItem jMenuItem3 = new JMenuItem();

  public SharePopupMenu(ShareAntPanel callerShare, FileInfos fileInfos) {
    super();
    this.callerShare = callerShare;
    this.fileInfos = fileInfos;
    try {
      jbInit();
    }
    catch(Exception e) {
      _logger.error("", e);
    }
  }

  private void jbInit() throws Exception {
    jMenuItem1.setText(ji.JI.i("Copy ants2k link to clipboard"));
    jMenuItem1.addMouseListener(new SharePopupMenu_jMenuItem1_mouseAdapter(this));
    jMenuItem2.setText(ji.JI.i("Copy ed2k link to clipboard"));
    jMenuItem2.addMouseListener(new SharePopupMenu_jMenuItem2_mouseAdapter(this));
    jMenuItem3.setText(ji.JI.i("Open links html page"));
    jMenuItem3.addMouseListener(new SharePopupMenu_jMenuItem3_mouseAdapter(this));
    this.add(jMenuItem1);
    this.add(jMenuItem2);
    this.add(jMenuItem3);
  }

  void jMenuItem1_mousePressed(MouseEvent e) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String fileName = (new File(this.fileInfos.getName())).getName();
    StringSelection data = new StringSelection("ants2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getHash()+"|/");
    clipboard.setContents(data, data);
    this.setVisible(false);
  }

  void jMenuItem2_mousePressed(MouseEvent e) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String fileName = (new File(this.fileInfos.getName())).getName();
    StringSelection data = new StringSelection("ed2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getED2KHash()+"|/");
    clipboard.setContents(data, data);
    this.setVisible(false);
  }

  void jMenuItem3_mousePressed(MouseEvent e) {
    String fileName = (new File(this.fileInfos.getName())).getName();
    String htmlPage = "<html><head><title>"+ji.JI.i("ANts P2P links page")+"</title></head><body bgcolor=\"#000000\"><p><table style=\"color: #0066CC; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt;\"><tr><td><img src=\"ed2k_1.gif\"/><td>"+fileName+"</tr><tr><td><td>"+this.fileInfos.getExtendedInfos()+"</tr><tr><td><img src=\"ed2k_2.gif\"/><td><a href=\"ed2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getED2KHash()+"|/\">ed2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getED2KHash()+"|/</a></tr><tr><td><img src=\"ed2k_2.gif\"/><td><a href=\"ants2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getHash()+"|/\">ants2k://|file|"+fileName+"|"+this.fileInfos.getSize()+"|"+this.fileInfos.getHash()+"|/</a></tr></p>";
    String[] args = new String[1];
    try{
      WarriorAnt.checked2kPath();
      PrintWriter pw = new PrintWriter(new FileWriter(WarriorAnt.ed2kPath + fileName+".html"));
      pw.write(htmlPage);
      pw.close();
      args[0] = (new File(WarriorAnt.ed2kPath + fileName + ".html")).getAbsolutePath();
      Runtime.getRuntime().exec("\""+HttpAntPanel.browserPath+"\""+" \""+args[0]+"\"");
    }catch(Exception ex){
      JOptionPane.showMessageDialog(this, ji.JI.i("Not found")+": "+HttpAntPanel.browserPath+"\n"+ji.JI.i("Please set your browser path in settings..."),ji.JI.i("Error in starting Browser"),JOptionPane.ERROR_MESSAGE);
      _logger.error("Error in starting: "+HttpAntPanel.browserPath+" "+args[0],ex);
    }
    this.setVisible(false);
  }
}

class SharePopupMenu_jMenuItem1_mouseAdapter extends java.awt.event.MouseAdapter {
  SharePopupMenu adaptee;

  SharePopupMenu_jMenuItem1_mouseAdapter(SharePopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    final MouseEvent event = e;
    final SwingWorker worker = new SwingWorker() {
      public Object construct() {
        adaptee.jMenuItem1_mousePressed(event);
        return null;
      }
    };
    worker.start();
  }
}

class SharePopupMenu_jMenuItem2_mouseAdapter extends java.awt.event.MouseAdapter {
  SharePopupMenu adaptee;

  SharePopupMenu_jMenuItem2_mouseAdapter(SharePopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem2_mousePressed(e);
  }
}

class SharePopupMenu_jMenuItem3_mouseAdapter extends java.awt.event.MouseAdapter {
  SharePopupMenu adaptee;

  SharePopupMenu_jMenuItem3_mouseAdapter(SharePopupMenu adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jMenuItem3_mousePressed(e);
  }
}
